package com.ruoyi.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ps 图片 添加原图与重命名后图片的 表格已使用状态列
 */
public class ImageFileNameComparator {
    private static final Logger logger = LoggerFactory.getLogger(ImageFileNameComparator.class);

    // 方法：递归获取指定文件夹下所有子文件夹中名称包含original的图片文件名列表
    // 参数：folderPath - 根文件夹路径
    // 返回值：包含符合条件图片文件名的List
    private List<String> getOriginalImageFiles(String folderPath) {
        List<String> originalImageFiles = new ArrayList<>();
        File rootFolder = new File(folderPath);
        if (!rootFolder.exists() || !rootFolder.isDirectory()) {
            logger.error("指定的文件夹不存在或不是一个文件夹: {}", folderPath);
            return originalImageFiles;
        }

        processDirectory(rootFolder, originalImageFiles);
        return originalImageFiles;
    }

    // 辅助方法：递归处理目录及其子目录
    private void processDirectory(File directory, List<String> imageFiles) {
        File[] files = directory.listFiles();
        if (files == null) {
            return;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                processDirectory(file, imageFiles);
            } else if (file.isFile() && file.getName().contains("original")) {
                imageFiles.add(file.getName());
            }
        }
    }

    // 方法：对比图片文件名列表与Excel表格第二列，并添加新列
    // 参数：imageFiles - 图片文件名列表，excelFilePath - Excel文件路径
    // 返回值：无
    private void compareAndUpdateExcel(List<String> imageFiles, String excelFilePath) {
        try (FileInputStream fis = new FileInputStream(new File(excelFilePath));
             XSSFWorkbook workbook = new XSSFWorkbook(fis)) {
            XSSFSheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 0; i <= lastRowNum; i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    Cell cell = row.getCell(1);
                    if (cell != null) {
                        String cellValue = cell.getStringCellValue();
                        if (imageFiles.contains(cellValue)) {
                            Cell newCell = row.createCell(4);
                            newCell.setCellValue("已引用");
                        }
                    }
                }
            }
            try (FileOutputStream fos = new FileOutputStream(new File(excelFilePath))) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            logger.error("处理Excel文件时发生错误", e);
        }
    }

    public static void main(String[] args) {
        ImageFileNameComparator comparator = new ImageFileNameComparator();
        // 假设图片文件夹路径为"./images"，请根据实际情况修改
        String imageFolderPath = "D:\\BaiduNetdiskDownload\\ps成品";
        // 假设Excel文件路径为"./data.xlsx"，请根据实际情况修改
        String excelFilePath = "D:\\BaiduNetdiskDownload\\ps\\pic_old_new.xlsx";
        List<String> originalImageFiles = comparator.getOriginalImageFiles(imageFolderPath);
        logger.info("找到 {} 个包含'original'的图片文件", originalImageFiles.size());
        comparator.compareAndUpdateExcel(originalImageFiles, excelFilePath);
        logger.info("图片文件名对比及Excel更新操作已完成");
    }
}


